<!--订单主表管理-->
<template>
  <div id="businessOrderHeader" class="app-container">
    <!--搜索条件-->
    <div class="filter-container">
      <el-form :model="listQuery" :inline="true">
        <el-form-item>
          <el-input v-model="listQuery.keyword" clearable placeholder="请输入关键字" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" plain @click="handleFilter">搜索</el-button>
          <el-button icon="el-icon-refresh" plain @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--列表-->
    <div class="table-container flex-main">
      <el-tabs v-model="listQuery.orderStatus" class="common-tabs" @tab-click="handleTabClick">
        <el-tab-pane label="已付款" name="2" />
        <el-tab-pane label="待付款" name="0" />
        <el-tab-pane label="付款失败" name="1" />
      </el-tabs>
      <div class="table-container-header">
        <table-toolbar :remove="false" :create="false" @refresh="handleFilter">
          <template>
            <el-button v-for="item in 5" :key="item" type="primary" plain @click="handleCreateOrder(item/10)">打赏 ￥{{ item/10 }}</el-button>
          </template>
        </table-toolbar>
      </div>
      <el-table
        ref="multipleTable"
        :data="list"
        header-cell-class-name="table-header"
        fit
        height="100%"
        highlight-current-row
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
        @sort-change="sortChange">
        <el-table-column align="center" prop="id" label="#" width="60">
          <template slot-scope="scope">{{ scope.$index+1 }}</template>
        </el-table-column>
        <el-table-column label="订单号" prop="id" align="center" :show-overflow-tooltip="true" width="200">
          <template slot-scope="scope">
            <span class="common-cursor color-primary">
              <copy-text :content="scope.row.id" position="left" />
            </span>
          </template>
        </el-table-column>
        <el-table-column label="订单名称" prop="orderName" min-width="300" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-if="listQuery.orderStatus==='2'" class="color-primary common-cursor" @click="handleLook(scope.row)">{{ scope.row.orderName }}</span>
            <span v-else>{{ scope.row.orderName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="orderDate" sortable label="下单时间" width="160">
          <template slot-scope="scope">{{ scope.row.gmtCreated | formatDate }}</template>
        </el-table-column>
        <el-table-column prop="grandTotal" sortable label="金额" width="120">
          <template slot-scope="scope"><span class="color-danger">￥{{ scope.row.grandTotal }}</span></template>
        </el-table-column>
        <el-table-column prop="userRealName" :show-overflow-tooltip="true" sortable label="真实姓名" width="100" />
        <el-table-column prop="userPhone" sortable label="电话" width="100" />
        <el-table-column label="操作" align="center" fixed="right" width="90" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <template v-if="scope.row.orderStatus===0">
              <button-option icon="el-icon-setting" skin="success" title="付款" @click="handleDoPay(scope.row)" />
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination_handler">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
    </div>
    <!--新增/编辑侧边栏-->
    <side-box :show="sideBox.recordShow" :title="sideBox.title" @close="sideBox.recordShow = !sideBox.recordShow">
      <template slot="body">
        <div class="common-title m-b-20"><h2 class="bold">订单信息</h2></div>
        <div class="businessOrderDetails">
          <el-row class="item" :gutter="10">
            <el-col :span="24">
              订单名称：{{ orderHeader.header.orderName }}
            </el-col>
          </el-row>
          <el-row class="item" :gutter="10">
            <el-col :span="12">
              订单号：<copy-text :content="orderHeader.header.id" position="left" />
            </el-col>
            <el-col :span="12">
              下单时间：{{ orderHeader.header.gmtCreated | formatDate }}
            </el-col>
          </el-row>
          <el-row class="item" :gutter="10">
            <el-col :span="12">
              状态：{{ orderHeader.header.orderStatus === 0 ? '待付款': orderHeader.header.orderStatus===1 ? '付款失败' :'已付款' }}
            </el-col>
            <el-col :span="12">
              支付金额：<span class="color-danger">￥{{ orderHeader.header.grandTotal }}</span>
            </el-col>
          </el-row>
          <el-row class="item" :gutter="10">
            <el-col :span="12">
              姓名：{{ orderHeader.header.userRealName }}
            </el-col>
            <el-col :span="12">
              手机号：{{ orderHeader.header.userPhone }}
            </el-col>
          </el-row>
          <div class="common-title m-b-20"><h2 class="bold">支付信息</h2></div>
          <el-row class="item" :gutter="10">
            <el-col :span="24">
              交易号：<copy-text :content="orderHeader.payRecord.tradeNo" position="left" />
            </el-col>
          </el-row>
          <el-row class="item" :gutter="10">
            <el-col :span="12">
              付款时间：{{ orderHeader.payRecord.gmtCreated | formatDate }}
            </el-col>
            <el-col :span="12">
              状态：{{ orderHeader.payRecord.payStatus === 0 ? '付款成功' :'付款失败' }}
            </el-col>
          </el-row>
          <el-row class="item" :gutter="10">
            <el-col :span="12">
              支付方式：{{ orderHeader.payRecord.tradeType }}
            </el-col>
            <el-col :span="12">
              支付渠道：{{ orderHeader.payRecord.channelPayway }}
            </el-col>
          </el-row>
          <el-row class="item" :gutter="10">
            <el-col :span="12">
              商户号：{{ orderHeader.payRecord.partnerId }}
            </el-col>
            <el-col :span="12">
              总费用：￥{{ orderHeader.payRecord.totalFee }}
            </el-col>
          </el-row>
        </div>
      </template>

    </side-box>
    <side-box :show="sideBox.payShow" :title="sideBox.title" @close="sideBox.payShow = !sideBox.payShow">
      <template slot="body">
        <pay ref="pay" @payNotify="handlePayNotify" />
      </template>
    </side-box>
  </div>
</template>

<script>
import Pay from '@/views/components/pay.vue'

export default {
  components: { Pay },

  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        keyword: undefined,
        sort: this.common.SORT_DEFAULT,
        orderStatus: '2'
      },

      multipleSelectionRecords: [],
      // 固定写法
      sideBox: {
        recordShow: false,
        payShow: false,
        title: ''
      },
      orderHeader: {
        header: {},
        payRecord: {}
      }
    }
  },
  watch: {
    'sideBox.payShow': function(val) {
      if (val === false) {
        this.$refs.pay.clearInterval()
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleTabClick() {
      this.handleFilter()
    },
    // 表格自带方法开始
    /* 全选/反选*/
    handleSelectionChange(val) {
      this.multipleSelectionRecords = val
    },
    /* 表格行数据单击*/
    handleRowClick(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    /* 表格列排序*/
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = this.common.SORT_DEFAULT
      }
      this.handleFilter()
    },
    // 表格自带方法结束

    /* 搜索*/
    handleFilter() {
      this.listQuery = {
        ...this.listQuery,
        page: 1
      }
      this.fetchData()
    },
    /* 重置*/
    handleReset() {
      this.listQuery = {
        ...this.listQuery,
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        keyword: undefined,
        sort: this.common.SORT_DEFAULT
      }
      this.fetchData()
    },
    async fetchData() {
      const response = await this.$store.dispatch('businessOrderHeaderPage', this.listQuery)
      this.list = response.records
      this.total = response.total
    },
    /* 新增*/
    handleCreateOrder(price) {
      this.$store.dispatch('businessCreateOrder', {
        orderName: '打赏',
        price: price
      }).then(response => {
        this.msgSuccess('订单创建成功,去支付！')
        this.handleReset()
        this.handleDoPay(response)
      }).catch(() => {
        this.sideBox.payShow = true
      })
    },
    handleDoPay(row) {
      this.sideBox.payShow = true
      this.sideBox.title = row.orderName
      this.$nextTick(() => {
        this.$refs.pay.init(row.id)
      })
    },
    handlePayNotify(val) {
      this.sideBox.payShow = false
      this.handleFilter()
    },
    handleLook(row) {
      this.$store.dispatch('businessOrderHeaderGetDetailsById', row.id).then((res) => {
        this.orderHeader = {
          header: res.orderHeader,
          payRecord: res.paymentRecord
        }
        this.sideBox = {
          recordShow: true,
          title: `订单【${row.orderName}】详情`
        }
      }).catch((_) => {})
    }

  }
}
</script>
<style lang="scss" scoped>
 .businessOrderDetails{
    margin-top: 15px;
    .item{
      margin-bottom: 12px;
      font-size: 14px;
      font-weight: 500;
      color: #333;
    }
    .jv-container .jv-code{
      padding: 5px !important;
    }
  }
</style>
